package GDoc::Job::Fetch::US::Exec::Ag::ERS::RuralAmerica;

use Moose;

use WWW::Mechanize;
use GDoc::Job;
use File::Temp;


extends 'GDoc::Job::Fetch';


sub setup { 
    my $self = shift;
    $self->_baseurl( 'http://www.ers.usda.gov/publications/RuralAmerica/archives/' );
    $self->_basedir( 'http://www.ers.usda.gov' );
}

sub run { 
    my $self = shift;
   
    if ( $self->data->{link} ) { 
        $self->_fetch_pdf;
    } else { 
        $self->logger->info( "fetching base " . $self->_baseurl );
        my $mech = $self->_mech;
        $mech->get( $self->_baseurl );
        
        my @links = $mech->find_all_links( text_regex => qr{Volume\s+\d{2},\s+No\.\s+\d}i );
        foreach my $link( @links ) { 
            $link->text =~ m{Volume\s+(?<vol>\d{2}),\s+No\.\s+(?<num>\d)}i;
            my ( $vol, $num ) = @+{ 'vol', 'num' };
            
            $self->logger->info( "[$vol] [$num] - following link " . $link->url );
            $mech->get( $link );
            
            my $ent_link = $mech->find_link( text_regex => qr{Entire}i );
            $mech->back;
            
            unless ( $ent_link ) { 
                $self->logger->info( "No entire issue/report link, skipping" );
                next;
            }
            
            my $job = GDoc::Job->new( class => 'Fetch::US::Exec::Ag::ERS::RuralAmerica',
                                      queue => $self->fetch_queue,
                                      data  => { link   => $ent_link->url,
                                                 vol    => $vol,
                                                 num    => $num
                                               }
                                    );
            $job->submit;

        }
    }
}

sub _fetch_pdf { 
    my $self = shift;
    my %data = %{ $self->data };

    my $ident = sprintf( 'ERS-RA-%02d-%02d', $data{vol}, $data{num} );
    my $filename = $ident . '.pdf';
    my $key = 'us/exec/ag/ers/' . $filename;

    $self->logger->info( "Storing $data{link} --> $key" );

    my $mech = $self->_mech;
    my $tmpf = File::Temp->new;
    $mech->get( $self->_basedir . $data{link}, ':content_file' => $tmpf->filename );

    $self->store_pdf( key      => $key,
                      datafile => $tmpf,
                      filename => $filename,
                      meta     => { description   => "Rural America, Volume $data{vol}, Issue $data{num}",
                                    identifier    => $ident,
                                  }
                    );

}


1;
